<?php

/**
 * @author Gideon Amissah<kuulmek@yahoo.com>
 */
class InfosController extends AppController {

    var $name = 'Infos';
    var $helpers = array('Form', 'Html', 'Session');
    var $components = array('RequestHandler', 'Session');
    # Models to be used
    var $uses = array(
        'Info',
        'TempScore'
    );

    function index() {
        
    }

    /**
     * This function register.
     * @name rigister
     * @access public
     */
    function register() {
        $this->__sendNotification();
        if (!empty($this->data)) {
            $result = $this->__beforeRegister();
            if ($result) {
                $numTest = $this->__getNumberOfTest($result['Info']['email']);
                if (count($numTest['ClientScore']) == 4) {
                    $myText = $result['Info']['email'];
                } else {

                    $message = "Dear Sir/Madam, <br /><br />" .
                            "This email is to notify you that you have just registered to take our" . "<br />" .
                            "PACEM Diagnostics test using your email address through which this message has been received" . "<br /><br />" .
                            "In case you are not the one that used your email address for the purpose above," . "<br />" .
                            "kindly inform us immediately using the contact details below." . "<br /><br />" .
                            "Email Addresses" . "<br />" .
                            "pacem@rtheconsult.com" . "<br />" .
                            "Tathompson@rtheconsult.com" . "<br />" . "<br />" .
                            "Phone Numbers" . "<br />" .
                            "Office lines - +233307033535, +233307033235 " . "<br />" .
                            "Mobile line - +233248324101." . "<br /><br />" .
                            "Thank you." . "<br /><br />" .
                            "RtHE Consult Ltd";

                    $headers = "MIME-Version: 1.0\r\n";
                    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
                    @mail($result['Info']['email'], 'PACEM Diagnostics Test Registration Notification', $message, $headers);

                    $this->Session->write('Client_id', $result['Info']['id']);
                    $this->redirect(array('controller' => 'Questions', 'action' => 'instructions'), null, true);
                    $this->Session->write('Client_data', $result);
                }
            } else {
                $this->Info->create();
                if ($this->Info->save($this->data)) {
                    $this->Session->setFlash('The Info has been saved');

                    $this->Session->write('Client_id', $this->Info->id);

                    $message = "Dear Sir/Madam, <br /><br />" .
                            "This email is to notify you that you have just registered to take our" . "<br />" .
                            "PACEM Diagnostics test using your email address through which this message has been received" . "<br /><br />" .
                            "In case you are not the one that used your email address for the purpose above," . "<br />" .
                            "kindly inform us immediately using the contact details below." . "<br /><br />" .
                            "Email Addresses" . "<br />" .
                            "pacem@rtheconsult.com" . "<br />" .
                            "Tathompson@rtheconsult.com" . "<br /><br />" .
                            "Phone Numbers" . "<br />" .
                            "Office lines - +233307033535, +233307033235 " . "<br />" .
                            "Mobile line - +233248324101." . "<br /><br />" .
                            "Thank you." . "<br /><br />" .
                            "RtHE Consult Ltd";

                    $headers = "MIME-Version: 1.0\r\n";
                    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
                    @mail($this->data['Info']['email'], 'PACEM Diagnostics Test Registration Notification', $message, $headers);

                    $this->redirect(array('controller' => 'Questions', 'action' => 'instructions'), null, true);
                } elseif ($this->data == "") {
                    $this->Session->setFlash('Info not saved. Try again.');
                    $this->redirect('/');
                }
            }
        }
        $this->set(compact('myText'));
    }

    /**
     * @This function checks if the user has already registered then loads his data if any.
     * @name beforeRigister
     * @access public
     */
    function __beforeRegister() {
        $email = $this->data['Info']['email'];
        $result = $this->Info->find('first', array(
            'conditions' => array('Info.email' => $email),
            'contain' => array('TempScore'),
            'recursive' => 1
                ));
        return $result;
    }

    function __getNumberOfTest($email = 0) {
        $email = $this->data['Info']['email'];
        $result = $this->Info->find('first', array(
            'conditions' => array('Info.email' => $email),
            'contain' => array('ClientScore'),
            'recursive' => 1
                ));
        return $result;
    }

    function __sendNotification() {
        $format = 'F j, Y, g:i a';
        $currentDate = strtotime(date('Y-m-d'));
        $clientData = $this->TempScore->find('all');

        foreach ($clientData as $clientDatas) {
            $date = DateTime::createFromFormat($format, $clientDatas['Info']['date_created']);
            $fetchDate = strtotime($date->format('Y-m-d'));

            $dateDiff = ($currentDate - $fetchDate);
            if ($dateDiff >= 604800) {
                echo $clientDatas['Info']['email'];
                $message = "Dear Sir/Madam, <br /><br />" .
                        "You used our PACEM Diagnostics business tool recently but were not able to finish all steps to get your free business analysis report" . "<br />" .
                        "Our system saved your unfinished PACEM Diagnostics test and you can conclude it at any time within a 30 day period from the date you started the PACEM Diagnostics test" . "<br /><br />" .
                        "You can return to the PACEM Diagnostics test by using the same login details – the tool will take you straight to the questions left to be answered having passed through the instructions page." . "<br />" .
                        "Kindly call us so we can discuss your report and we look forward to assisting you in taking your organisation to greater heights." . "<br /><br />" .
                        "Email Addresses" . "<br />" .
                        "pacem@rtheconsult.com" . "<br />" .
                        "Tathompson@rtheconsult.com" . "<br /><br />" .
                        "Phone Numbers" . "<br />" .
                        "Office lines - +233307033535, +233307033235 " . "<br />" .
                        "Mobile line - +233248324101." . "<br /><br />" .
                        "Thank you." . "<br /><br />" .
                        "RtHE Consult Ltd";

                $headers = "MIME-Version: 1.0\r\n";
                $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
                @mail($clientDatas['Info']['email'], 'PACEM Diagnostics Test Registration Notification', $message, $headers);
            } elseif ($dateDiff >= 1209600) {
                //Bi-Weekly Mail here
                echo $clientDatas['Info']['email'];
                $message = "Dear Sir/Madam, <br /><br />" .
                        "You used our PACEM Diagnostics business tool recently but were not able to finish all steps to get your free business analysis report" . "<br />" .
                        "Our system saved your unfinished PACEM Diagnostics test and you can conclude it at any time within a 30 day period from the date you started the PACEM Diagnostics test" . "<br /><br />" .
                        "You can return to the PACEM Diagnostics test by using the same login details – the tool will take you straight to the questions left to be answered having passed through the instructions page." . "<br />" .
                        "Kindly call us so we can discuss your report and we look forward to assisting you in taking your organisation to greater heights." . "<br /><br />" .
                        "Email Addresses" . "<br />" .
                        "pacem@rtheconsult.com" . "<br />" .
                        "Tathompson@rtheconsult.com" . "<br /><br />" .
                        "Phone Numbers" . "<br />" .
                        "Office lines - +233307033535, +233307033235 " . "<br />" .
                        "Mobile line - +233248324101." . "<br /><br />" .
                        "Thank you." . "<br /><br />" .
                        "RtHE Consult Ltd";

                $headers = "MIME-Version: 1.0\r\n";
                $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
                @mail($clientDatas['Info']['email'], 'PACEM Diagnostics Test Registration Notification', $message, $headers);
            } elseif ($dateDiff >= 1814400) {
                //Tri-Weekly Mail Here

                $message = "Dear Sir/Madam, <br /><br />" .
                        "You used our PACEM Diagnostics business tool recently but were not able to finish all steps to get your free business analysis report" . "<br />" .
                        "Our system saved your unfinished PACEM Diagnostics test and you can conclude it at any time within a 30 day period from the date you started the PACEM Diagnostics test" . "<br /><br />" .
                        "You can return to the PACEM Diagnostics test by using the same login details – the tool will take you straight to the questions left to be answered having passed through the instructions page." . "<br />" .
                        "Kindly call us so we can discuss your report and we look forward to assisting you in taking your organisation to greater heights." . "<br /><br />" .
                        "Email Addresses" . "<br />" .
                        "pacem@rtheconsult.com" . "<br />" .
                        "Tathompson@rtheconsult.com" . "<br /><br />" .
                        "Phone Numbers" . "<br />" .
                        "Office lines - +233307033535, +233307033235 " . "<br />" .
                        "Mobile line - +233248324101." . "<br /><br />" .
                        "Thank you." . "<br /><br />" .
                        "RtHE Consult Ltd";

                $headers = "MIME-Version: 1.0\r\n";
                $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
                @mail($clientDatas['Info']['email'], 'PACEM Diagnostics Test Registration Notification', $message, $headers);
            } elseif ($dateDiff >= 2592000) {
                //Delete Temaporary Test
                $this->TempScore->delete($clientDatas['TempScore']['info_id'] , $cascade = true);
            }
        }
    }

}

?>
